package com.wudd.exception;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import com.wudd.common.bean.response.ResponseVO;

@RestControllerAdvice
public class GlobalExceptionHandler {
	private Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
	
	/**
     * 全局异常捕捉处理
     * @param ex
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    public ResponseVO<Object> errorHandler(Exception ex) {
        return ResponseVO.error(ex.getMessage());
    }
    
    /**
     * 拦截捕捉自定义异常 MyException.class
     * @param ex
     * @return
     */
    @ExceptionHandler(value = MyHandlerException.class)
    public ResponseVO<Object> myErrorHandler(MyHandlerException ex) {
    	logger.error("MyHandlerException info:{}",ex.getMsg());
        return ResponseVO.error(ex.getMsg());
    }
    
    /**
     * 用于字段校验
     * @param ex
     * @return
     */
    @ExceptionHandler(value = BindException.class)
    public ResponseVO<Object> BindExceptionErrorHandler(BindException ex) {
    	logger.error("BindException 字段校验info:{}",ex.getMessage());
        return ResponseVO.error(ex.getBindingResult().getFieldError().getDefaultMessage());
    }
}
